#ifndef _MQTT_HANDLER_H_
#define _MQTT_HANDLER_H_


#include "MsgDefine.h"
#include "AiotEntity.h"
#include "MQTT/Params.h"
#include "MQTT/Message.h"
#include "Core/Utils/List.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief mqtt buffer 定义
 */
typedef struct {
    uint8_t    *buffer;
    uint32_t    len;
} MQTTBuffer;

typedef enum {
    mqttDestroy
}MQTTEventType;

typedef struct {
    MQTTEventType type;
} MQTTEvent;

typedef void (*MQTTProcessHandler)(void *context, AiotEvent *event, MQTTEvent *mqttEvent);

/**
 * @brief mqtt 实体
 */
typedef struct {

    AiotEntity              aiotEntity;

    MQTTControlParams       controlInfo;
    RecvHandler             recvHandler;
    char*                   username;
    char*                   password;
    char*                   clientId;
    char*                   extendClientId;
    uint8_t                 cleanSession;
    uint8_t                 appendRequestId;
    uint32_t                rePublishTimeout;
    uint32_t                reSubscribeTimeout;
    uint8_t                 reSendSubscribeEnabled;
    uint8_t                 hasConnected;
    uint8_t                 disconnected;
    uint8_t                 disconnectApiCalled;
    uint16_t                packetId;
    void*                   subMutex;
    void*                   pubMutex;
    void*                   processMutex;
    ListHead                subList;
    ListHead                pubList;
    ListHead                processList;

    void*                   topicAliasMutex;
    ListHead                rxTopicAliasList;
    ListHead                txTopicAliasList;
    uint8_t*                preConnectProperty;
    uint32_t                txPacketSize;
    uint32_t                txTopicAliasSize;
    uint32_t                txTopicAlias;
    uint16_t                serverReceiveMax;
    uint8_t                 flowControlEnabled;
} MQTTEntity;

typedef struct {
    MQTTProperty            type;
    uint16_t                len;
    uint8_t*                value;
} ValueLength;

/**
 * @brief 上下行报文中所能携带属性
 */
typedef struct {
    uint8_t         maxQos;
    uint16_t        maxTopicAlias;           /** 服务端/设备端能够支持的topic alias最大值 */
    uint8_t*        assignedCliId;
    uint32_t        maxPacketSize;
    uint8_t         wildSubAvailable;
    uint8_t         subIdentifierAvailable;
    uint8_t         sharedSubAvailable;
    uint32_t        msgExpireInterval;
    uint16_t        topicAlias;
    MQTTValue       responseTopic;          /**response topic */
    MQTTValue       correlationData;        /**关联数据 */
    uint32_t        subIdentifier;
    UserProperty*   userProperty[MAX_USER_PROPERTY];   /**用户属性的列表*/
    uint16_t        serverReceiveMax;         /** 服务端的qos1流控值 */
    uint16_t        maxClientReceive;         /** 设备端的qos1流控值 */
    MQTTValue*      reasonString;
} GeneralProperty;

#define DEFAULT_SERVER_RECEIVE_MAX             (10)

#ifdef __cplusplus
}
#endif

#endif //_MQTT_HANDLER_H_
